package homework12月2号;

import java.util.ArrayList;
import java.util.Comparator;

public class H2 {
  private String name;
  private int age;
  private int KG;

    @Override
    public String toString() {
        return "H2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", KG=" + KG +
                '}';
    }

    public H2() {
    }

    public H2(String name, int age, int KG) {
        this.name = name;
        this.age = age;
        this.KG = KG;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getKG() {
        return KG;
    }

    public void setKG(int KG) {
        this.KG = KG;
    }

    public static void main(String[] args) {
        ArrayList<H2> hlist = new ArrayList<>();
        hlist.add(new H2("哈士奇",1,3));
        hlist.add(new H2("藏獒",2,12));
        hlist.add(new H2("博美",2,6));
        hlist.add(new H2("柴犬",3,12));
        System.out.println("排序前"+hlist);
        hlist.sort(new Comparator<H2>() {
            @Override
            public int compare(H2 o1, H2 o2) {
                int a1 = o1.age;
                int a2 = o2.age;
                if (a1>a2){
                    return -1;
                }else if (a1==a2){
                    if (o1.KG>o2.KG){
                        return -1;
                    }
                    return 1;
                }else {
                    return 1;
                }
            }
        });
        System.out.println("排序后"+hlist);
    }
}
